package org.flower.web.servlet;

import com.github.pagehelper.PageInfo;
import org.flower.bean.Order;
import org.flower.bean.OrderState;
import org.flower.service.OrderService;
import org.flower.service.OrderStateService;
import org.flower.service.impl.OrderServiceImpl;
import org.flower.service.impl.OrderStateServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 订单列表servlet
 *
 * @author Aizen
 * @date 2021/12/24
 */
@WebServlet("/manager/orderListServlet.shtml")
public class OrderListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageNo = request.getParameter("pageNo");
        if(pageNo==null||pageNo.equals("")){
            pageNo= "1";
        }
        OrderService os = new OrderServiceImpl();

        //获取状态表
        OrderStateService oss = new OrderStateServiceImpl();
        List<OrderState> stateList = oss.findAllState();
        request.setAttribute("stateList",stateList);

        PageInfo<Order> orderPageInfo = os.findAllOrder(null,Integer.parseInt(pageNo));
        request.setAttribute("orderPageInfo",orderPageInfo);
        request.getRequestDispatcher("orderList.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageNo = request.getParameter("pageNo");
        if(pageNo==null||pageNo.equals("")){
            pageNo= "1";
        }

        //获取状态表
        OrderStateService oss = new OrderStateServiceImpl();
        List<OrderState> stateList = oss.findAllState();
        request.setAttribute("stateList",stateList);

        Order order = new Order();
        //模糊查询
        //按订单编号查询
        String id = request.getParameter("id");
        order.setId(id);
        //按订单状态查询
        String stateid = request.getParameter("stateid");
        if(stateid!=null&&!stateid.equals("")){
            order.setOrderstate(Integer.parseInt(stateid));
        }
        //收货人手机号
        String shouphone = request.getParameter("shouphone");
        if(shouphone!=null&&!shouphone.equals("")){
            order.setShouphone("%"+shouphone+"%");
        }
        //下单日期
        String orderdate = request.getParameter("orderdate");
        if(orderdate!=null&&!orderdate.equals("")){
            order.setOrderdateStr("%"+orderdate+"%");
        }


        OrderService os = new OrderServiceImpl();
        PageInfo<Order> orderPageInfo = os.findAllOrder(order,Integer.parseInt(pageNo));
        request.setAttribute("orderPageInfo",orderPageInfo);
        request.getRequestDispatcher("orderList.jsp").forward(request,response);

    }
}
